package com.yougou.ui.address;

import android.content.Intent;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.data.model.Address;
import com.yougou.utils.AddressManager;
import com.yougou.utils.ToastUtils;

public class AddressEditActivity extends BaseActivity {
    
    public static final String EXTRA_ADDRESS = "extra_address";
    
    private EditText mEtName;
    private EditText mEtPhone;
    private EditText mEtProvince;
    private EditText mEtCity;
    private EditText mEtDistrict;
    private EditText mEtDetail;
    private CheckBox mCbDefault;
    private Button mBtnSave;
    
    private Address mAddress;
    private boolean mIsEdit = false;
    
    @Override
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_edit);
        
        // 获取传入的地址
        mAddress = (Address) getIntent().getSerializableExtra(EXTRA_ADDRESS);
        mIsEdit = mAddress != null;
        
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        // 设置标题
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(mIsEdit ? "编辑地址" : "添加地址");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        
        mEtName = findViewById(R.id.et_name);
        mEtPhone = findViewById(R.id.et_phone);
        mEtProvince = findViewById(R.id.et_province);
        mEtCity = findViewById(R.id.et_city);
        mEtDistrict = findViewById(R.id.et_district);
        mEtDetail = findViewById(R.id.et_detail);
        mCbDefault = findViewById(R.id.cb_default);
        mBtnSave = findViewById(R.id.btn_save);
        
        // 保存按钮
        mBtnSave.setOnClickListener(v -> saveAddress());
    }
    
    @Override
    protected void initData() {
        if (mIsEdit && mAddress != null) {
            // 编辑模式，填充数据
            mEtName.setText(mAddress.getName());
            mEtPhone.setText(mAddress.getPhone());
            mEtProvince.setText(mAddress.getProvince());
            mEtCity.setText(mAddress.getCity());
            mEtDistrict.setText(mAddress.getDistrict());
            mEtDetail.setText(mAddress.getDetail());
            mCbDefault.setChecked(mAddress.isDefault());
        }
    }
    
    
    private void saveAddress() {
        String name = mEtName.getText().toString().trim();
        String phone = mEtPhone.getText().toString().trim();
        String province = mEtProvince.getText().toString().trim();
        String city = mEtCity.getText().toString().trim();
        String district = mEtDistrict.getText().toString().trim();
        String detail = mEtDetail.getText().toString().trim();
        boolean isDefault = mCbDefault.isChecked();
        
        // 验证
        if (name.isEmpty()) {
            ToastUtils.show(this, "请输入收货人姓名");
            return;
        }
        if (phone.isEmpty()) {
            ToastUtils.show(this, "请输入手机号");
            return;
        }
        if (phone.length() != 11) {
            ToastUtils.show(this, "手机号格式不正确");
            return;
        }
        if (province.isEmpty() || city.isEmpty() || district.isEmpty()) {
            ToastUtils.show(this, "请输入省市区信息");
            return;
        }
        if (detail.isEmpty()) {
            ToastUtils.show(this, "请输入详细地址");
            return;
        }
        
        if (mIsEdit && mAddress != null) {
            // 编辑模式
            mAddress.setName(name);
            mAddress.setPhone(phone);
            mAddress.setProvince(province);
            mAddress.setCity(city);
            mAddress.setDistrict(district);
            mAddress.setDetail(detail);
            mAddress.setDefault(isDefault);
            AddressManager.updateAddress(mAddress);
            ToastUtils.show(this, "地址已更新");
        } else {
            // 新增模式
            Address address = new Address(name, phone, province, city, district, detail);
            address.setDefault(isDefault);
            AddressManager.addAddress(address);
            ToastUtils.show(this, "地址已添加");
        }
        
        setResult(RESULT_OK);
        finish();
    }
    
    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}

